package com.zm.zmgz.utils;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/04/04/13:47
 * @Description: 字符串相关工具方法
 */
public class StringCjwUtils {

    public static void main(String[] args) {
        System.err.println(getPathName("/"));
    }

    /***
    * @Description: 百分号字符串转小数
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-13 13:35:05
    */
    public static double getDoubleByPercent(String s){
        s = s.substring(0,s.length()-1);
        return Double.parseDouble(s) / 100;
    }


    /***
     * @Description: 获取路径名
     * 参数要求:"/"开头和结尾,否则返回原样返回
     * 测试: /a/b/c/:c    /:/     /a/b:/a/b       /a/:a
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-11 15:07:39
     */
    public static String getPathName(String path){

        // 检查格式
        if(!path.startsWith("/") || !path.endsWith("/"))
            return path;
        String[] split = path.split("/");
        if(split.length < 2) { // 根目录的父目录为根目录,一级目录的父目录也为根目录
            return "/";
        }
        // 返回父目录名
        return split[split.length-1];
    }

    /***
    * @Description: 获取父路径
     * 参数要求:"/"开头和结尾,否则返回原样返回
     * 测试: /a/b/c/:/a/b/    /:/     /a/b:/a/b       /a/:/
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-11 15:07:39
    */
    public static String getParentPath(String path){

        // 检查格式
        if(!path.startsWith("/") || !path.endsWith("/"))
            return path;
        String[] split = path.split("/");
        if(split.length < 2) { // 根目录的父目录为根目录,一级目录的父目录也为根目录
            return "/";
        }
        // 拼接出父目录
        String parentPath = "";
        for (int i = 0; i < split.length - 1; i++) {
            parentPath += split[i] + "/";
        }
        return parentPath;
    }


    /**
     * 比较两个字符串的大小，按字母的ASCII码比较
     * @param pre
     * @param next
     * @return
     * */
    public static int isMoreThan(String pre, String next){
        if(null == pre || null == next || "".equals(pre) || "".equals(next)){
            System.err.println("字符串比较数据不能为空！");
            return -1;
        }

        char[] c_pre = pre.toCharArray();
        char[] c_next = next.toCharArray();

        int minSize = Math.min(c_pre.length, c_next.length);

        for (int i = 0; i < minSize; i++) {
            if((int)c_pre[i] > (int)c_next[i]){
                return 1;
            }else if((int)c_pre[i] < (int)c_next[i]){
                return -1;
            }
        }
        if(c_pre.length > c_next.length){
            return 1;
        }

        return -1;
    }

    /***
    * @Description: 根据位数,在字符串前补零; 获取number的位数,遍历添加"0",遍历次数为两个位数之差
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-04 13:53:23
    */
   public static String zeroPadding(int digit,String number){
       if(Objects.isNull(number)) {
           System.out.println("补零字符串为空!");
           return null;
       }
       int length = number.length();
       String temp = number;
       if(digit <= length) {
           System.out.println(number+"位数大于或等于"+digit+",不需要在前面补零!");
           return number;
       } else {
           for (int i = 0; i < digit-length; i++){
               number = "0" + number;
           }
           System.out.println(temp+"补零后:"+number);
           return number;
       }
   }


   /***
   * @Description: 去除数字前缀(0012-xxx -> xxx)
   * @Param:
   * @return:
   * @Author: cjw
   * @Date: 2020-04-04 15:13:48
   */
   public static String removeNumericPrefix(String s) {
       if(Objects.isNull(s)) {
           System.out.println("传入字符串为空!");
           return null;
       }
       String[] split = s.split("^(\\d)+-");
       if(Objects.isNull(split) || split.length < 2) {
           System.out.println(s+"数字前缀匹配失败!");
           return s;
       } else {
           System.out.println(s+"数字前缀匹配成功,以去除前缀!");
           return split[1];
       }
   }


   /***
   * @Description: 通过数值字符串获取double
   * @Param:
   * @return:
   * @Author: cjw
   * @Date: 2020-06-02 21:18:27
   */
    public static Double getDoubleByString(String amount) {
        if (Objects.isNull(amount))
            return null;
        return Double.valueOf(amount);
    }
}
